package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.response.LoginResponse;
import cn.fintecher.pangolin.entity.mysql.AssistCase;
import cn.fintecher.pangolin.entity.mysql.QAssistCase;
import cn.fintecher.pangolin.service.business.model.request.AssistCaseDistributeRequest;
import cn.fintecher.pangolin.service.business.model.request.AssistCaseSearchRequest;
import cn.fintecher.pangolin.service.business.model.response.AssistCaseDistributeResponse;
import cn.fintecher.pangolin.service.business.model.response.AssistCaseResponse;
import cn.fintecher.pangolin.service.business.repository.jparepository.AssistCaseRepository;
import cn.fintecher.pangolin.service.business.service.AssistCaseService;
import cn.fintecher.pangolin.service.business.service.BaseService;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/assistCaseController")
@Api(value = "外访协催案件管理", description = "外访协催案件管理")
@Slf4j
public class AssistCaseController {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private BaseService baseService;

    @Autowired
    private AssistCaseService assistCaseService;

    @Autowired
    private AssistCaseRepository assistCaseRepository;

    @ApiOperation(value = "催收管理查询协催案件", notes = "催收管理查询协催案件")
    @GetMapping("/searchAssistCollectionCase")
    public ResponseEntity<Page<AssistCaseResponse>> searchAssistCollectionCase(AssistCaseSearchRequest request, Pageable pageable, @RequestHeader(value = "X-UserToken") String token) {
        log.info("search wait collection cases");
        BooleanBuilder booleanBuilder = request.generateQueryBuilder();
        LoginResponse loginResponse = baseService.getUserSessionByToken(token);
        if (loginResponse.getUserModel().getIsManage().equals(BaseSelect.YES)) {
            booleanBuilder.and(QAssistCase.assistCase.assistCollector.department.departCode.like(StringUtils.join(loginResponse.getDepartmentModel().getDepartCode(), "%")));
        } else {
            booleanBuilder.and(QAssistCase.assistCase.assistCollector.id.eq(loginResponse.getUserModel().getId()));
        }
        Sort sort = new Sort(Sort.Direction.ASC, "visitTimeStart", "operatorTime");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<AssistCase> assistCasePage = assistCaseRepository.findAll(booleanBuilder, pageable);
        return ResponseEntity.ok().body(assistCasePage.map(assistCollectionCase -> modelMapper.map(assistCollectionCase, AssistCaseResponse.class)));
    }

    @PutMapping("/endAssistCaseCollecting/{assistId}")
    @ApiOperation(value = "结束协催", notes = "结束协催")
    public ResponseEntity endAssistCaseCollecting(@PathVariable String assistId) {
        assistCaseService.endAssistCaseCollect(assistId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/assistCaseDistributePre")
    @ApiOperation(value = "外访分案预览", notes = "外访分案预览")
    public ResponseEntity<AssistCaseDistributeResponse> assistCaseDistributePre(@RequestBody AssistCaseDistributeRequest request) throws BadRequestException {
        AssistCaseDistributeResponse response = assistCaseService.manualAssistDistributeCase(request);
        return ResponseEntity.ok().body(response);
    }

    @PostMapping("/assistConfirmDistributeCase")
    @ApiOperation(value = "外访分案确认", notes = "外访分案确认")
    public ResponseEntity assistConfirmDistributeCase(@RequestBody AssistCaseDistributeResponse request) throws BadRequestException {
        assistCaseService.confirmAssistDistributeCase(request);
        return ResponseEntity.ok().body(null);
    }
}
